package com.itheima.service;


import com.itheima.common.SnowFlake;
import com.itheima.mapper.admin.CategoryMapper;
import com.itheima.mapper.admin.DishMapper;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.pojo.admin.Category;
import com.itheima.pojo.admin.SetmealDish;
import com.itheima.pojo.admin.SetmealRelation;
import com.itheima.pojo.client.SelectSetmealByCategoryIdAndStatusVo;
import com.itheima.pojo.client.selectDishListVo.SelectDishListVo;
import com.itheima.pojo.client.selectSetmealDishByIdVo.SelectSetmealDishByIdVo;
import com.itheima.pojo.client.selectSetmealDishByIdVo.SelectSetmealDishFlavorListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 套餐模块service层
 */
@Service
public class SetmealService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    //通过套餐id查询菜品(菜品详情 )
    public List<SelectSetmealDishByIdVo> selectSetmealDishById(String setmealId){

        //查询套餐id所绑定的套餐菜品关系
        List<SetmealRelation> setmealRelations = setmealMapper.setmealRelationSelectById(setmealId);

            //通过套餐关系中的菜品id查询菜品
            List<SelectSetmealDishByIdVo> dishList = dishMapper.selectSetmealDishById(setmealRelations);

            //通过菜品关系查询对应分量
            for (SelectSetmealDishByIdVo dish : dishList) {
                //遍历菜品关系
                for (SetmealRelation setmealRelation : setmealRelations) {
                    //循环对比关系与查询菜品的id，如果一样则将份数添加上去
                    if (setmealRelation.getDishId().equals(dish.getId())){
                        dish.setCopies(setmealRelation.getCopies());
                    }
                }
            }


            //通过分类id查询对应分类名
            for (SelectSetmealDishByIdVo dish : dishList) {
                Category category = categoryMapper.categorySelectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
            }

            //通过菜品id集合查询对应的口味
            List<SelectSetmealDishFlavorListVo> selectSetmealDishFlavorListDtos = dishMapper.selectSetmealDishFlavorList(dishList);

            for (SelectSetmealDishByIdVo selectSetmealDishByIdDto : dishList) {
                selectSetmealDishByIdDto.setFlavors(selectSetmealDishFlavorListDtos);
            }



        return dishList;
    }


    //通过分类id与状态查询套餐
    public List<SelectSetmealByCategoryIdAndStatusVo> selectSetmealByCategoryIdAndStatus(String categoryId,Integer status){
        return setmealMapper.selectSetmealByCategoryIdAndStatus(categoryId, status);
    }
}
